package com.site.action.backend.dining;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.site.dto.dining.RestaurantBizBean;
import com.site.service.dining.RestaurantService;
import com.site.util.Code;
import com.site.util.CodeMapDBUtil;
import com.site.util.base.action.BaseAction;
import com.site.util.base.service.BaseService;
import com.site.util.common.fileupload.FileUploadHandle;
import com.site.util.constant.Constants;

/**
 * 餐厅
 * @author jack
 *
 */
public class RestaurantAction extends BaseAction {

	private static final long serialVersionUID = -630458404524017846L;

	private RestaurantService restaurantService;
	
	private List<RestaurantBizBean> restaurantBizBeans;
	
	private RestaurantBizBean restaurantBizBean;
	
	// 餐厅类型
	private List<Code> codes;
	
	public List<Code> getCodes() {
		return codes;
	}
	public void setCodes(List<Code> codes) {
		this.codes = codes;
	}
	public List<RestaurantBizBean> getRestaurantBizBeans() {
		return restaurantBizBeans;
	}
	public void setRestaurantBizBeans(List<RestaurantBizBean> restaurantBizBeans) {
		this.restaurantBizBeans = restaurantBizBeans;
	}
	@Resource
	public void setRestaurantService(RestaurantService restaurantService) {
		this.restaurantService = restaurantService;
	}
	public RestaurantBizBean getRestaurantBizBean() {
		return restaurantBizBean;
	}
	public void setRestaurantBizBean(RestaurantBizBean restaurantBizBean) {
		this.restaurantBizBean = restaurantBizBean;
	}

	/**
	 * 根据条件获得符合条件的数据
	 * @return
	 */
	public String getRestaurants(){
		if(null == queryPage){
			super.initPagination();
		}
		init();
		restaurantBizBeans = restaurantService.getRestaurants(queryPage, restaurantBizBean);
		return "getRestaurants";
	}
	
	/**
	 * 增加页面
	 * @return
	 */
	public String getRestaurantAdd(){
		init();
		return "getRestaurantAdd";
	}
	/**
	 * 数据初始化
	 */
	private void init() {
		codes = CodeMapDBUtil.getCodeByCodeName(Constants.RESTAURANT_TYPE, request);
		if(null == codes || codes.isEmpty()){
			codes = new ArrayList<Code>();
		}
	}
	
	/**
	 * 增加
	 * @return
	 */
	public String restaurantAdd(){
		if(checkInput(restaurantBizBean)){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("restaurantBizBean", restaurantBizBean);
			// 上传对象 start 
			FileUploadHandle fileUploadHandle = new FileUploadHandle();
			fileUploadHandle.setRequest(request);
			// 取得上传文件类型
			fileUploadHandle.setAllowedTypes(Constants.UPDATE_FILE_TYPES);
			// 取得上传文件大小
			fileUploadHandle.setMaxSize(Constants.FILE_SIZE);
			fileUploadHandle.setFiles(files);
			fileUploadHandle.setFilesName(getFilesFileName());
			// 上传对象 end
			map.put("fileUploadHandle", fileUploadHandle);
			restaurantService.execute(map, BaseService.COMMAND_ADD);
			super.setMessageID("SYS_MSG_00022");
		} else {
			return getRestaurantAdd();
		}
		
		super.setBackurl(request.getContextPath() + "/restaurant/restaurantAction!getRestaurants.do");
		return "toMessage";
	}
	
	/**
	 * 根据id查询相应数据
	 * @return
	 */
	public String getRestaurantInfo(){
		return "getRestaurantInfo";
	}
	
	/**
	 * 根据id查询相应数据修改页面
	 * @return
	 */
	public String getRestaurantEditPage(){
		if(null != restaurantBizBean){
			restaurantBizBean = restaurantService.getRestaurantById(restaurantBizBean);
		}
		init();
		return "getRestaurantEditPage";
	}
	
	/**
	 * 修改方法
	 * @return
	 */
	public String restaurantEdit(){
		if(null != restaurantBizBean && restaurantBizBean.getId() != null && checkInput(restaurantBizBean)){
			restaurantService.execute(restaurantBizBean, BaseService.COMMAND_UPDATE);
			super.addFieldError("success", "数据更新成功！");
		} else {
			super.addFieldError("success", "数据更新失败，请认真查看错误提示！");
		}
		init();
		return "getRestaurantEditPage";
	}
	
	private Boolean checkInput(RestaurantBizBean restaurantBizBean){
		boolean mark = true;
		if(null != restaurantBizBean){
			if(StringUtils.isEmpty(restaurantBizBean.getName())){
				super.addFieldError("name", "餐厅名称不能为空！");
				mark = false;
			}
			if(StringUtils.isEmpty(restaurantBizBean.getAddress())){
				super.addFieldError("address", "地址不能为空！");
				mark = false;
			}
			if(StringUtils.isEmpty(restaurantBizBean.getLongitude())){
				super.addFieldError("longitude", "精度不能为空！");
				mark = false;
			}
			if(StringUtils.isEmpty(restaurantBizBean.getLatitude())){
				super.addFieldError("latitude", "纬度不能为空！");
				mark = false;
			}
			if(StringUtils.isEmpty(restaurantBizBean.getLatitude())){
				super.addFieldError("latitude", "纬度不能为空！");
				mark = false;
			}
			// 宴席
			if(Constants.FEAST.equals(restaurantBizBean.getRestaurantType())){
				if(restaurantBizBean.getFeastPrice() == null || restaurantBizBean.getFeastPrice() == 0){
					super.addFieldError("feastPrice", "宴席价格不能为空,或者是0！");
					mark = false;
				}
				if(restaurantBizBean.getAccommodate() == null || restaurantBizBean.getAccommodate() == 0){
					super.addFieldError("accommodate", "容纳桌席不能为空,或者是0！");
					mark = false;
				}
			} else {
				if(restaurantBizBean.getAverageConsumption() == null || restaurantBizBean.getAverageConsumption() == 0){
					super.addFieldError("averageConsumption", "人均消费不能为空,或者是0！");
					mark = false;
				}
			}
			if(StringUtils.isEmpty(restaurantBizBean.getTEL())){
				super.addFieldError("TEL", "订餐电话不能为空！");
				mark = false;
			}
			if(restaurantBizBean.getUseArea() == null || restaurantBizBean.getUseArea() == 0){
				super.addFieldError("useArea", "使用面积不能为空！");
				mark = false;
			}
			if(null == restaurantBizBean.getBusinessHour()){
				super.addFieldError("businessHour", "营业时间不能为空！");
				mark = false;
			}
			if(StringUtils.isEmpty(restaurantBizBean.getSuperintendent())){
				super.addFieldError("superintendent", "负责人不能为空！");
				mark = false;
			}
			if(null == restaurantBizBean.getSuperintendent()){
				super.addFieldError("superintendent", "负责人不能为空！");
				mark = false;
			}
			if(StringUtils.isEmpty(restaurantBizBean.getSummary())){
				super.addFieldError("summary", "简介不能为空！");
				mark = false;
			}
		} else {
			mark = false;
		}
		return mark;
	}
}
